/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.structuralload.StructuralLoad;
import org.openaecfm.model.structuralload.StructuralLoadConfiguration;
import org.openaecfm.model.structuralload.StructuralLoadOrResult;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class StructuralLoadConfigurationIO extends
		AbstractEntityInstanceInputImpl<StructuralLoadConfiguration> implements
		IEntityInstanceOutput<StructuralLoadConfiguration> {

	private IEntityInstanceUtilOutput<StructuralLoad> utilOutput;
	private IEntityInstanceUtilInput<StructuralLoad> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<StructuralLoad> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<StructuralLoad> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(StructuralLoadConfiguration pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSTRUCTURALLOADCONFIGURATION", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set Values
		ListParameter values = new ListParameter();
		for (StructuralLoadOrResult s : pojo.getValues()) {
			values.add(new EntityInstanceNameParameter(appDataManager
					.getName(s)));
		}
		ei.addParameter(values);

		// Set Locations
		if (pojo.getLocations() == null || pojo.getLocations().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter locations = new ListParameter();
			for (List<Double> ld : pojo.getLocations()) {
				ListParameter lp = new ListParameter();
				for (Double d : ld) {
					lp.add(new RealParameter(d));
				}
				locations.add(lp);
			}
			ei.addParameter(locations);
		}

		return ei.toString();
	}

	@Override
	protected StructuralLoadConfiguration doCreateObject() {
		return new StructuralLoadConfiguration();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			StructuralLoadConfiguration pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Values
		ListParameter values = (ListParameter) sei.getParameter(1);
		for (Parameter p : values) {
			EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
			pojo.getValues().add(
					(StructuralLoadOrResult) appDataManager.getObject(e.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			StructuralLoadConfiguration pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Locations
		Parameter locations = sei.getParameter(2);
		if (locations.getNodeType() == NodeType.LIST) {
			ListParameter lp = (ListParameter) locations;
			for (Parameter p : lp) {
				ListParameter l = (ListParameter) p;
				List<Double> ld = new ArrayList<Double>();
				for (Parameter pa : l) {
					RealParameter rp = (RealParameter) pa;
					ld.add(rp.toDouble());
				}
				if (!ld.isEmpty()) {
					pojo.getLocations().add(ld);
				}
			}
		}
	}
}
